package com.lhr.common.dao.domain;

import java.util.ArrayList;
import java.util.List;
import com.lhr.common.domain.AbstractBean;
import com.lhr.common.domain.Order;


public class OrderByClause extends AbstractBean {

	private static final long	serialVersionUID	= 1L;
	private AbstractExample		example;
	private List<String>		sorts				= new ArrayList<String>();
	private List<Order>			orders				= new ArrayList<Order>();

	public OrderByClause() {
		super();
	}

	public OrderByClause(AbstractExample example) {
		super();
		this.example = example;
	}

	public OrderByClause add(String sort, Order order) {
		if (example != null)
			sorts.add(example.converter(sort));
		else
			sorts.add(sort);
		orders.add(order);
		return this;
	}

	/**
	 * default Order = desc
	 */
	public OrderByClause add(String sort) {
		return add(sort, Order.DESC);
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < sorts.size(); i++) {
			String sort = sorts.get(i);
			Order order = orders.get(i);
			sb.append(sort);
			sb.append(" ");
			sb.append(order == null ? Order.DESC.name() : order.name());
			if (i < sorts.size() - 1)
				sb.append(",");
		}
		return sb.toString();
	}
}
